#!/bin/bash

# usage: bash compare_two_lineups.sh schedule_file_name new_lineup_file_name old_lineup_file_name
# Examples:
# bash compare_two_lineups.sh /metadata/Redbee/be/20130903190005samsung.xml BE_Telenet_TVA_Lineups_20130530_v8.xml BE_Telenet_TVA_Lineups_20130530v2.xml
# bash compare_two_lineups.sh /metadata/Redbee/nl/20130903190306samsung_.xml NL_Ziggo_TVA_Lineups_20130411v15.xml NL_Ziggo_TVA_Lineups_20130411v5.xml

source /home/gentoo/lineup_check/compare_versions/func_module.eclass

function main {
  local SCHEDULE_FILE_XML=$1
  #SCHEDULE_FILE="/metadata/Redbee/be/20130903190005samsung.xml"
  local SCHEDULE_FILE_ONLY=`basename $SCHEDULE_FILE_XML`
  local SCHEDULE_FILE="${SCHEDULE_FILE_ONLY%.*}"

  local LINEUP_FILE_NEW_XML=$2
  #LINEUP_FILE_NEW_XML="BE_Telenet_TVA_Lineups_20130530_v8.xml"
  local LINEUP_FILE_ONLY_NEW=`basename $LINEUP_FILE_NEW_XML`
  local LINEUP_FILE_NEW="${LINEUP_FILE_ONLY_NEW%.*}"

  local LINEUP_FILE_OLD_XML=$3
  #LINEUP_FILE_OLD_XML="BE_Telenet_TVA_Lineups_20130530v2.xml"
  local LINEUP_FILE_ONLY_OLD=`basename $LINEUP_FILE_OLD_XML`
  local LINEUP_FILE_OLD="${LINEUP_FILE_ONLY_OLD%.*}"

  local OUTPUTDIR="comp-"$LINEUP_FILE_NEW"-"$LINEUP_FILE_OLD
  if [ ! -d "$OUTPUTDIR" ]; then
    mkdir $OUTPUTDIR 
  fi

  # Get lineups details from new and old lineups files.
  get_headend_info $LINEUP_FILE_NEW_XML > ${OUTPUTDIR}/${LINEUP_FILE_ONLY_NEW}_Lineups.txt
  get_headend_info $LINEUP_FILE_OLD_XML > ${OUTPUTDIR}/${LINEUP_FILE_ONLY_OLD}_Lineups.txt

  # check the change of headend list including MSO id, Headend id, Headend name and so on.
  echo "--------- Checking change of Headend info ------------"
  diff ${OUTPUTDIR}/${LINEUP_FILE_ONLY_OLD}_Lineups.txt ${OUTPUTDIR}/${LINEUP_FILE_ONLY_NEW}_Lineups.txt

  # Get common headend lists 
  join ${OUTPUTDIR}/${LINEUP_FILE_ONLY_NEW}_Lineups.txt ${OUTPUTDIR}/${LINEUP_FILE_ONLY_OLD}_Lineups.txt > ${OUTPUTDIR}/common_lineups.tmp

  get_channel_list_in_schedule_file $SCHEDULE_FILE_XML > ${OUTPUTDIR}/${SCHEDULE_FILE}_channels.txt

  # Compare common headend contents between old and new files.
  for common_line in `cat ${OUTPUTDIR}/common_lineups.tmp |awk -F "|" '{print $2"|"$3}'`
  do
    #echo $common_line
    HEADEND_ID=`echo $common_line | awk -F "|" '{print $1}' | perl -pe 's/srid:\/\/bds.tv\///g'`
    DEVICE_TYPE_CODE=`echo $common_line | awk -F "|" '{print $2}'`

    echo "******** Headend id: $HEADEND_ID DEVICE_TYPE: $DEVICE_TYPE_CODE ******************"
    check_each_lineup $LINEUP_FILE_NEW_XML $HEADEND_ID $DEVICE_TYPE_CODE > ${OUTPUTDIR}/lineup_${LINEUP_FILE_NEW}_${HEADEND_ID}_${DEVICE_TYPE_CODE}_ALL.txt
    check_each_lineup $LINEUP_FILE_OLD_XML $HEADEND_ID $DEVICE_TYPE_CODE > ${OUTPUTDIR}/lineup_${LINEUP_FILE_OLD}_${HEADEND_ID}_${DEVICE_TYPE_CODE}_ALL.txt

    # Check schedule channel change
    check_schedule_channels ${OUTPUTDIR}/${SCHEDULE_FILE}_channels.txt ${LINEUP_FILE_NEW_XML} ${LINEUP_FILE_OLD_XML} $HEADEND_ID $DEVICE_TYPE_CODE $OUTPUTDIR

    # Check for zip code mapping
    check_zipcode_mapping $LINEUP_FILE_NEW_XML $LINEUP_FILE_OLD_XML ${HEADEND_ID} $DEVICE_TYPE_CODE $OUTPUTDIR

    # Check LCN or Triplet(with Frequency) change
    check_lcn_triplet_freq_data ${LINEUP_FILE_NEW_XML} ${LINEUP_FILE_OLD_XML} $HEADEND_ID $DEVICE_TYPE_CODE $OUTPUTDIR
  done
}

# $1: SCHEDULE_FILE_XML $2: $LINEUP_FILE_NEW_XML $3: $LINEUP_FILE_OLD_XML
main $1 $2 $3
